﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Betop.Lib.Data;

namespace Betop.Lib.Core.Tool
{
    public class SysTable
    {
        private DBHelper _dbOper;
        private string _tableName;

        public SysTable(string connectionString,string tableName)
        {
            _dbOper = DBHelper.Instance(connectionString);
            _tableName = tableName;
        }
        public List<ColumnInfo> GetColumns()
        {
            string sql = "select '" + _tableName + "' as TableName, Name,IsNullable,Length,(select value from sys.extended_properties where major_id=syscolumns.id and minor_id=syscolumns.colid) as Description from syscolumns where syscolumns.id = object_id('" + _tableName + "')";
            List<ColumnInfo> cols = _dbOper.Query<ColumnInfo>(sql);
            return cols;
        }

        /// <summary>
        /// 获得空表的列架构
        /// </summary>
        /// <returns></returns>
        public DataTable GetSchema()
        {
            string sql = "select * from [" + _tableName.TrimStart('[').TrimEnd(']') + "] where 1=2";
            DataTable dt = _dbOper.Query(sql);
            return dt;
        }
        
    }
}
